package shop.controller.manage.dictionary;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import shop.core.BaseController;
import shop.core.LoginUserHolder;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.core.common.dao.page.PagerModel;
import shop.services.manage.dictionary.DictionarySerivice;
import shop.services.manage.dictionary.bean.Dictionary;
import shop.services.manage.system.bean.User;

import javax.servlet.http.HttpServletRequest;

/**
 * 字典管理Controller层
 **/

@Controller
@RequestMapping("/manage/dictionary/")
public class DictionaryAction extends BaseController<Dictionary>{
    private static final Logger logger = LoggerFactory.getLogger(DictionaryAction.class);
    //注入service层
    @Autowired
    private DictionarySerivice dictionarySerivice;

    private static final String page_toList = "/manage/dictionary/dictionaryList";
    private static final String page_toAdd = "/manage/dictionary/dictionaryAdd";
    private static final String page_toEdit = "/manage/dictionary/dictionaryEdit";

    //继承父类的final变量，通过赋值让BaseController层知道跳转路径
    private DictionaryAction(){
        super.page_toList = page_toList;
        super.page_toAdd = page_toAdd;
        super.page_toEdit = page_toEdit;
    }
   //重写方法，用于调用service接口的方法
    @Override
    public Services<Dictionary> getService() {
        return dictionarySerivice;
    }
    /**
     * 更新方法，更新成功跳转到列表页面
     */
    @Override
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(HttpServletRequest request, @ModelAttribute("e") Dictionary e, RedirectAttributes flushAttrs) throws Exception {
        getService().update(e);
        return page_toList;
    }

    /**
     * 更改显示/隐藏状态
    * */
    @RequestMapping(value = "changeStatus",method = RequestMethod.POST)
    @ResponseBody  //    用于将Controller的方法返回的对象，通过适当的HttpMessageConverter转换为指定格式后，写入到Response对象的body数据区。一般用于json格式中
    public JSONResult changeStatus(HttpServletRequest request, @RequestParam(value = "idArr[]") String[] ids){
        JSONResult rs =new JSONResult();
        User user = LoginUserHolder.getLoginUser();         //用户登陆--权限
        String	status=request.getParameter("type");
        dictionarySerivice.updateDictionaryStatus(ids, status, user.getUsername());
        rs.setSuccess(true);
        return rs;
    }
 /*   *//**
     * 分页获取数据列表
     *
     * @param request 请求
     * @param e       对象参数
     * @return 分页数据模型
     *//*
    @Override
    @RequestMapping("loadData")
    @ResponseBody  // 用于将Controller的方法返回的对象，通过适当的HttpMessageConverter转换为指定格式后，写入到Response对象的body数据区。一般用于json格式中
    public PagerModel loadData(HttpServletRequest request, Dictionary e) {
        int offset = 0;                     //每一页开始的偏移量
        int pageSize = 10;                  //每一页的个数
        if (e.getDictName()!=null){
            //模糊查询
            e.setDictType(e.getDictName());
        }
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if (pageSize < 0) {
            pageSize = 10;
        }
        e.setOffset(offset);
        e.setPageSize(pageSize);
        PagerModel pager = getService().selectPageList(e);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }*/
}
